package com.author.surface;

import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.List;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.ColorModel;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * This class will be used to fetch all the properties related to the users
 * system e.g. - Graphics and Font information.
 */
public final class SystemProperty {
	/** Generic System properties */
	public static String	SYSTEM_TYPE;
	public static int		NUMBER_OF_CPUS		= 0;
	public static String	OS_NAME;
	public static String	OS_VERSION;

	/** System UI properties */
	public static int		SCREEN_WIDTH		= 0;
	public static int		SCREEN_HEIGHT		= 0;
	public static int		NUMBER_OF_SCREENS	= 0;
	public static int		SCREEN_DPI			= 0;
	public static final int	NUMBER_OF_COLORS	= 256;

	/** System Font information */
	public static String[]	SYSTEN_FONT_INFO;

	public SystemProperty() {
		init();
	}

	private void init() {
		setSystemProperties();
		setSystemUIProperties();
		setSystemTextProperties();
	}

	/**
	 * Set system properties such as - OS Name, Version and Machine type
	 * (Windows/UNIX/Linux/Mac)
	 */
	private void setSystemProperties() {

	}

	/**
	 * Sets all system UI related information
	 */
	private void setSystemUIProperties() {
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice[] gd = ge.getScreenDevices();
		SystemProperty.NUMBER_OF_SCREENS = gd.length;

		/*
		 * Get the default screen device, and the associated default Graphics
		 * configuration
		 */
		GraphicsDevice gd2 = ge.getDefaultScreenDevice();
		GraphicsConfiguration gc = gd2.getDefaultConfiguration();
		Rectangle bounds = gc.getBounds();
		SystemProperty.SCREEN_WIDTH = bounds.width;
		SystemProperty.SCREEN_HEIGHT = bounds.height;
	}

	/**
	 * Get the name of all the fonts present in the user's system and store this
	 * information in a sorted order in a list.
	 */
	private void setSystemTextProperties() {
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		Font[] font = ge.getAllFonts();
		java.util.List<String> fontList = new LinkedList<String>();
		for (int i = 0; i < font.length; i++) {
			fontList.add(font[i].getFamily());
		}

		/*
		 * fontList will have duplicates, the following code will remove
		 * duplicates from the list of font names
		 */
		int count = 0;
		Hashtable<String, Integer> table = new Hashtable<String, Integer>();
		for (String name : fontList) {
			if (table.containsKey(name)) continue;
			table.put(name, count++);
		}
		int i = 0;
		String[] tt = new String[table.keySet().size()];
		Iterator<String> it = table.keySet().iterator();
		for (; it.hasNext();)
			tt[i++] = it.next();
		fontList = new LinkedList<String>();
		for (i = 0; i < tt.length; i++)
			fontList.add(tt[i]);
		Collections.sort(fontList);

		/*
		 * Put the sorted list back into the Array. This will allow indexed
		 * accessing of the font names.
		 */
		if (SystemProperty.SYSTEN_FONT_INFO == null)
			SystemProperty.SYSTEN_FONT_INFO = new String[fontList.size()];
		for (i = 0; i < fontList.size(); i++) {
			SystemProperty.SYSTEN_FONT_INFO[i] = fontList.get(i);
		}
	}

	private GraphicsEnvironment getGraphicsEnvironment() {
		return GraphicsEnvironment.getLocalGraphicsEnvironment();
	}

	private GraphicsDevice getDefaultGraphicsDevice() {
		return GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getDefaultScreenDevice();
	}

	private GraphicsConfiguration getDefaultGraphicsConfiguration() {
		return GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getDefaultScreenDevice().getDefaultConfiguration();
	}
}
